layui.use(['layer','form', 'button', 'popup', 'echarts','element', 'count'], function () {
        var $ = layui.jquery,
            layer = layui.layer,
            element = layui.element,
            count = layui.count,
            echarts = layui.echarts,
            common  = layui.common,
            form    = layui.form,
            button  = layui.button,
            table   = layui.table;
            
            $('#noticeToggle').on('click', function(){
                var $hidden = $('.notice-hidden');
                var $btn = $(this).find('.toggle-btn');
                var $icon = $btn.find('.layui-icon');
                var $text = $btn.find('.toggle-text');
                
                if($hidden.is(':visible')){
                    $hidden.slideUp();
                    // 修改图标旋转样式
                    $icon.css('transform', 'rotate(0deg)');
                    $text.text('展开更多');
                } else {
                    $hidden.slideDown();
                    // 修改图标旋转样式
                    $icon.css('transform', 'rotate(180deg)');
                    $text.text('收起');
                }
            });
            
            // 如果公告少于等于3条，隐藏展开按钮
            var visibleItems = $('#noticeList > li').length;
            if(visibleItems <= 1){
                $('#noticeToggle').hide();
            }
            $(document).ready(function () {
                $.ajax({
            		url: './ajax/consumeall',
            		dataType: 'json',
            		type: 'POST',
            		success: function(res) {
            			if (res.code == 200) {
            				consumeall = res.data;
            				data(consumeall);
            			}
            		},
            		error:function(){
            		
            			return false;
            		}
            	});
                let cols = [
    				[{
    					field: "id",
    					title: "序号",
    					unresize: "true",
    					align: "center"
    				}, {
    					field: 'typename',
    					title: '支付方式',
    					align: 'center',
    				}, {
    					field: 'beizhu',
    					title: '通道备注',
    					align: 'center',
    				},{
    					field: "status",
    					title: "状态",
    					unresize: "true",
    					templet: "#qrliststatus",
    					align: "center"
    				}, {
    					field: "money",
    					title: "总金额",
    					unresize: "true",
    					align: "center"
    				},{
    					field: "succmoney",
    					title: "总收款金额",
    					unresize: "true",
    					align: "center"
    				},{
    					field: "order",
    					title: "总订单",
    					align: "center"
    				},{
    					field: "succorder",
    					title: "总完成订单",
    					align: "center"
    				}]
    			]
    			table.render({
    				elem: '#type_list',
    				id: 'type_list',
    				url: './ajax/maintypegetDataList',
    				page: true,
    				cols: cols,
    				cellMinWidth: 100,
    				skin: 'line',
    				toolbar: '#toolbar',
    				defaultToolbar: [{
    					title: '刷新',
    					layEvent: 'refreshtype',
    					icon: 'layui-icon-refresh',
    				}, 'filter', 'print', 'exports'],
    				parseData: function(res){
    				    if(res.send == 1){
    				        window.refreshtype();
    				    }
    				}
    			});
                table.on('toolbar(type_list)', function(obj) {
    			    if (obj.event === 'refreshtype') {
    					window.refreshtype();
    				}
    			});
    			// 修改状态
    			form.on("switch(qrliststatus)", function(data) {
        			var id = this.value;
        			var load = layer.load();
        			$.ajax({
    					data: {
    					    id:id
    					},
    					url: "./ajax/qrliststatus",
    					dataType: 'json',
    					type: 'post',
    					success: function(res) {
    					    layer.close(load);
    						if (res.code == 200) {
    							layer.msg(res.msg, {
    								icon: 1,
    								time: 1000
    							}, function() {
                                    layui.table.reload("type_list");
    							});
    						} else {
    							layer.msg(res.msg, {
    								icon: 2,
    								time: 1000
    							},function(){
    							    $(data.elem).prop("checked", !$(data.elem).prop("checked"));
    						        form.render()
    							});
    						}
    					},
                        error: function() {
                            layer.close(load);
                            layer.msg("服务器错误！", { icon: 2, time: 1000 },function(){layui.table.reload("type_list");});
                        }
    				});
                });
    		let cols1 = [
				[{
					field: "id",
					title: "二维码ID",
					unresize: "true",
					align: "center"
				}, {
					field: "today_count",
					title: "今日订单",
					unresize: "true",
					align: "center"
				}, {
					field: "week_count",
					title: "本周订单",
					unresize: "true",
					align: "center"
				},{
					field: "month_count",
					title: "本月订单",
					unresize: "true",
					align: "center"
				},{
					field: "today_money",
					title: "今日金额",
					unresize: "true",
					align: "center"
				},{
					field: "week_money",
					title: "本周金额",
					align: "center"
				},{
					field: "month_money",
					title: "本月金额",
					align: "center"
				},{
					field: "today_succmoney",
					title: "今日完成金额",
					width:150,
					unresize: "true",
					align: "center"
				},{
					field: "week_succmoney",
					title: "本周完成金额",
					width:150,
					align: "center"
				},{
					field: "month_succmoney",
					title: "本月完成金额",
					width:150,
					align: "center"
				}]
			]
            
			table.render({
                elem: '#pay_list',
                id: 'pay_list',
                url: './ajax/mainpaygetDataList',
                page: true,
                cols: cols1, 
                cellMinWidth: 100,
                skin: 'line',
                toolbar: '#toolbarpay',
                defaultToolbar: [{
                    title: '刷新',
                    layEvent: 'refreshpay',
                    icon: 'layui-icon-refresh',
                }, 'filter', 'print', 'exports'],
				parseData: function(res){
				    if(res.send == 1){
				        window.refreshtype();
				    }
				}
            });
            table.on('toolbar(pay_list)', function(obj) {
				if (obj.event === 'refreshpay') {
					window.refreshpay();
				}
			});
        });
    			// 弹出窗设置 自己设置弹出百分比
        function screen() {
            var width = $(window).width() * 0.8;
            var height = $(window).height() * 0.8;
            return [width + 'px', height + 'px'];
        }

        // 刷新表格
        window.refreshtype = function () {
            table.reload('type_list');
        };
        window.refreshpay = function () {
            table.reload('pay_list');
        };
    	function data(consumeall){
    	    $("#value1").text(consumeall.money);
    	    $("#value2").text(consumeall.succorder);
    	    $("#value3").text(consumeall.succmoney);
    	    $("#value4").text(consumeall.order);
    	    
    	}
    	function openNoticeModal() {
            layer.open({
                type: 1,
                title: false,
                closeBtn: 1,
                anim: 1,
                area: ['420px', 'auto'],
                shadeClose: true,
                shade: [0.1, '#fff'],  
                skin: 'layui-layer-rim',
                content: $('#noticeModal'),
                offset: '120px',
                success: function(layero, index){
                    // 绑定关闭按钮事件
                    $('#closeModal').on('click', function(){
                        layer.close(index);
                    });
                }
            });
        }
        
        // 检查状态并决定是否显示弹窗
        function checkStatusAndShowModal() {
            $.ajax({
                url: './main/status',
                type: 'GET',
                dataType: 'json',
                success: function(res) {
                    if(res.code === 200) {
                        openNoticeModal();
                    }
                },
                error: function(xhr, status, error) {
                    console.log('Status check failed:', error);
                }
            });
        }
    
        checkStatusAndShowModal();
        
    });